package com.zero.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisTest {

    @Test
    public void testJedis(){
        // 1. 创建Jedis连接对象（port可以省略，默认是6379端口）
        Jedis jedis = new Jedis("192.168.213.100", 6379);

        // 2. 验证密码
        jedis.auth("123456");

        // 3.ping验证
        /*System.out.println(jedis.ping());
        System.out.println(jedis.info());*/

        // String数据类型操作
        System.out.println("--------------String类型操作------------------");
        jedis.select(15);  //选择数据库
        jedis.set("username","Zero");
        jedis.set("hobby","watching movie");
        jedis.append("hobby"," coding");
        System.out.println("username: " + jedis.get("username") + "; hobbies: " + jedis.get("hobby"));
        jedis.incr("support");
        jedis.incrBy("support",1000);
        System.out.println("返回的数据类型：" + jedis.get("username").getClass().getName());
        System.out.println("获取点赞数量：" + jedis.get("support"));

        // List类型操作
        System.out.println("--------------List类型操作------------------");
        /*jedis.lpush("instrument","guitar","violin","piano","drum","flute");*/
        List<String> instruments = jedis.lrange("instrument", 0, -1);
        System.out.println("返回的数据类型：" + instruments.getClass().getName());
        System.out.println(instruments);
        /*jedis.lpop("instrument");
        jedis.rpop("instrument");*/
        System.out.println(jedis.lrange("instrument",0,-1));

        // Hash类型操作
        System.out.println("--------------Hash类型操作------------------");
        jedis.hset("city:id:1","name","重庆");
        jedis.hset("city:id:1","age","2000");
        jedis.hset("city:id:1","specialties","山水相融");
        jedis.hset("city:id:1","weather","热");
        Map<String, String> cityInfo = jedis.hgetAll("city:id:1");
        System.out.println("返回的数据类型：" + cityInfo.getClass().getName());
        System.out.println("cityInfo: " + cityInfo);
        System.out.println(jedis.hget("city:id:1","name"));
        System.out.println(jedis.hget("city:id:1","weather"));
        jedis.hdel("city:id:1","weather");
        System.out.println("cityInfo: " + jedis.hgetAll("city:id:1"));

        // Set类型操作: 无序不重复
        System.out.println("--------------Set类型操作------------------");
        jedis.sadd("boys","黄牛","铁柱","棒槌","大脑壳","水娃");
        jedis.sadd("men","黄牛","铁柱","棒槌","火鸡公","穿山甲");
        Set<String> boys = jedis.smembers("boys");
        Set<String> men = jedis.smembers("men");
        System.out.println("返回的数据类型：" + boys.getClass().getName());
        System.out.println("boys" + boys);
        System.out.println("men" + men);
        System.out.println("Intersection: " + jedis.sinter("boys","men"));
        System.out.println("Union: " + jedis.sunion("boys","men"));
        System.out.println("boysDiff: " + jedis.sdiff("boys","men"));
        System.out.println("menDiff: " + jedis.sdiff("men","boys"));

        // SortedSet类型操作
        System.out.println("--------------SortedSet类型操作------------------");
        jedis.zadd("scientists",1.0,"牛顿");
        jedis.zadd("scientists",2.0,"麦克斯韦");
        jedis.zadd("scientists",6.0,"法拉第");
        jedis.zadd("scientists",7.0,"屠呦呦");
        jedis.zadd("scientists",3.0,"钱学森");
        jedis.zadd("scientists",4.0,"詹天佑");
        Set<String> scientists = jedis.zrange("scientists", 0, -1);
        System.out.println("返回的数据类型：" + scientists.getClass().getName());
        System.out.println(scientists);
        System.out.println("人数：" + jedis.zcard("scientists"));

        System.out.println(jedis.zrangeByScore("scientists",2.0,6.0));

    }
}
